package healththink

class FacilityUserLink{
    User user
    Facility facility
    UserType userType

    static constraints = {
		//facility(nullable:true)
        userType(nullable:true)
        //user(nullable:true)
    }
	

    static mapping = {
        //facility lazy:false
        //user lazy:false
    }

	  
    static FacilityUserLink link(User u, Facility f)
    {
        FacilityUserLink ful = FacilityUserLink.findByUserAndFacility(
                u,
                f
            )
        if (!ful)
        {
			ful = new FacilityUserLink()
			u.addToFacilities(ful)
            f.addToFacilityUsers(ful)
			ful.save()
        }
        return ful
    }
    
    static void unlink(User u, Facility f)
    {
        FacilityUserLink ful = FacilityUserLink.findByUserAndFacility(
                u,
                f
            )
            
        if (ful)
        {
            //u?.removeFromFacilities(ful)
            //f?.removeFromFacilitiyUsers(ful)
            ful.delete()
        }
    }
}